﻿using Bilibili.Common.Api;
using Bilibili.Model.Home;
using Bilibili.Model.Resource;
using Bilibili.WebClient.Common;
using Newtonsoft.Json.Linq;
using PCLUntils.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class IntroductionClient : ClientBase
    {
        public async Task<ReturnModel<JObject>> Attention(string mid, string mode)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"act={mode}&fid={mid}&re_src=32");
                var results = await ApiClient.PostResults("https://api.bilibili.com/x/relation/modify", dataInfo.ToString());
                var obj = results.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> Like(string aid)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"dislike=0&like=0&aid={aid}", ApiProviderBase.Build5);
                var results = await ApiClient.PostResults("https://app.bilibili.com/x/v2/view/like", dataInfo.ToString());
                var obj = results.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> DisLike(string aid)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"aid={aid}&dislike=0&like=0");
                var results = await ApiClient.PostResults("https://app.bilibili.com/x/v2/view/dislike", dataInfo.ToString());
                var obj = results.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> Triple(string aid)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"aid={aid}");
                var results = await ApiClient.PostResults("https://app.bilibili.com/x/v2/view/like/triple", dataInfo.ToString());
                var obj = results.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> Collect(string aid, string fid)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"aid={aid}&fid={fid}&re_src=90", ApiProviderBase.Build5, true);
                string result = await ApiClient.PostResults("http://api.bilibili.com/x/v2/fav/video/add", dataInfo.ToString());
                var obj = result.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> SubmitCoin(int count, string aid)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"aid={aid}&from=7&mid={ApiProvider.Instance.CurrentUserId}&multiply={count}", ApiProviderBase.Build5);
                string result = await ApiClient.PostResults("https://app.bilibili.com/x/v2/view/coin/add", dataInfo.ToString());
                var obj = result.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<List<FavboxModel>>> GetFavBox()
        {
            try
            {
                string results = await ApiClient.GetResults("http://api.bilibili.com/x/favourite/folder?jsonp=jsonp&rnd=" + Current.Instance.Radom.Next(1, 9999));
                var ban = results.ParseObject<FavboxModel>()?.data.ParseObject<List<FavboxModel>>();
                return BuildSuccessResult(ban);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<List<FavboxModel>>(ex);
            }
        }
    }
}